package com.pdstudio.actualtime;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import com.pdstudio.weather.R;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;

public class MaxMinRedaActivity extends Activity {

	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			if (msg.what == 1) {
				// 成功,做一些操作
				PushSoureToImageView();
			} else {
				// 失败,..
			}
		}

	};

	ScrollView sv;
	LinearLayout ll;
	List<Bitmap> bms;
	int bitmapWidth, bitmapHeight;
	LinearLayout.LayoutParams imagebtn_params;

	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		setContentView(R.layout.actualtime_maxmin_reda_main);

		// sv = (ScrollView) findViewById(R.id.actualtime_reda_mami_scrollview);
		ll = (LinearLayout) findViewById(R.id.actualtime_reda_mami_ly);
		bms = new ArrayList<Bitmap>();
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);

		bitmapWidth = dm.widthPixels / 2;
		bitmapHeight = dm.widthPixels / 2;
		imagebtn_params = new LinearLayout.LayoutParams(bitmapWidth,
				bitmapHeight);
		imagebtn_params.topMargin = 20;

		new Thread() {
			public void run() {
				String[] path = new String[] {
						"http://113.247.234.237/uploads/Avatars/000/003/280/3280.jpg-small.jpg",
						"http://113.247.234.237/uploads/Avatars/000/003/280/3280.jpg-small.jpg",
						"http://113.247.234.237/uploads/Avatars/000/003/280/3280.jpg-small.jpg",
						"http://113.247.234.237/uploads/Avatars/000/003/280/3280.jpg-small.jpg",
						"http://113.247.234.237/uploads/Avatars/000/003/280/3280.jpg-small.jpg" };

				for (int i = 0; i < path.length; i++) {
					Bitmap bitmap = getHttpBitmap(path[i]);
					PushBitmapToBitmaps(bitmap);
				}
				Message m = new Message();
				m.what = 1;
				handler.sendMessage(m);
			}
		}.start();

	}

	private synchronized void PushBitmapToBitmaps(Bitmap bm) {
		if (bms != null && bm != null) {
			// Matrix matrix = new Matrix();
			// matrix.postScale(scaleWidth, scaleHeight);
			// Bitmap temp =
			// Bitmap.createBitmap(bm,0,0,bitmapWidth,bitmapHeight,);
			// if(temp!=null){
			bms.add(bm);
			// }
		}
	}

	private synchronized void PushSoureToImageView() {
		if (bms != null && ll != null) {
			for (int i = 0; i < bms.size(); i++) {
				try {
					Bitmap bm = bms.get(i);
					ImageView iv = new ImageView(this);
					iv.setLayoutParams(imagebtn_params);
					// iv.setScaleType(ScaleType.CENTER);// 居中显示

					// Bitmap bitmap = getLoacalBitmap("/aa/aa.jpg");//从本地取图片

					// Bitmap bitmap = getHttpBitmap(path[i]);// 从网上取图片

					iv.setImageBitmap(bm); // 设置Bitmap
					ll.addView(iv);

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 
	 * 加载本地图片
	 * 
	 * @param url
	 * 
	 * @return
	 */

	public static Bitmap getLoacalBitmap(String url) {

		try {

			FileInputStream fis = new FileInputStream(url);

			return BitmapFactory.decodeStream(fis);

		} catch (FileNotFoundException e) {

			e.printStackTrace();

			return null;

		}

	}

	/**
	 * 
	 * 从服务器取图片
	 * 
	 * @param url
	 * 
	 * @return
	 */

	public static Bitmap getHttpBitmap(String url) {

		URL myFileUrl = null;

		Bitmap bitmap = null;

		try {

			// Log.d(TAG, url);

			myFileUrl = new URL(url);

		} catch (MalformedURLException e) {

			e.printStackTrace();

		}

		try {

			HttpURLConnection conn = (HttpURLConnection) myFileUrl
					.openConnection();

			conn.setConnectTimeout(0);

			conn.setDoInput(true);

			conn.connect();

			InputStream is = conn.getInputStream();

			bitmap = BitmapFactory.decodeStream(is);

			is.close();

		} catch (Exception e) {

			e.printStackTrace();

		}

		return bitmap;

	}

}
